package SEApp.components {
	import SEApp.se_util;
	
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_loader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class se_source extends uif_ecomponent {
		protected var _source		: uif_loader;
		protected var _bitmap		: Bitmap;
		protected var _imageSize	: Point;
		
		public function se_source(parent:uif_component, ax:int=0, ay:int=0, 
								  awidth:int=0, aheight:int=0, styleClass: Object=null) {
			super(parent, ax, ay, awidth, aheight, styleClass);
			
			_imageSize = new Point();
			var inner: Rectangle = getInner();
			_source = new uif_loader(this, '', doLoad, inner.x, inner.y, app.loaderContext);
			addChild(_bitmap = new Bitmap());
			_source.visible = false;
		}
		
		protected function doLoad(e: Event): void {
			_imageSize = new Point(_source.width, _source.height);
			centeredSource();
		}
		
		protected function centeredSource(): void {
			if (_source) {
				var coordinates: Rectangle = EnterHere(_imageSize);
/*				_source.x 		= coordinates.x;
				_source.y 		= coordinates.y;
				_source.width 	= coordinates.width;
				_source.height 	= coordinates.height;
				*/				
				if (source.bitmapContentAccess) {
					se_util.bmpCopy(_source, coordinates.width / _source.contentLoaderInfo.width, _bitmap);
					_bitmap.x = coordinates.x;
					_bitmap.y = coordinates.y;
				} else {
					_source.x 		= coordinates.x;
					_source.y 		= coordinates.y;
					_source.width 	= coordinates.width;
					_source.height 	= coordinates.height;
					_source.visible = true;
					_bitmap.visible = false;
				}
			}
		}
		
		protected function drawSourceToBitmap(): void {
			se_util.bmpCopy(_source, coordinates.width / _source.contentLoaderInfo.width, _bitmap);
		}
		
		public function unload(): void {
			if (_bitmap.bitmapData) _bitmap.bitmapData.dispose();
			_source.unload();
		}
				
		public function get sourceScale(): Number {
			return _source.scaleX;
		}
		
		public function get source(): uif_loader {
			return _source;
		}
		
		public function get imageSize(): Point {
			return _imageSize;
		}
		
		public function get contentSize(): Point {
			return new Point(_source.contentLoaderInfo.width, _source.contentLoaderInfo.height);	
		}
		
		public function loadSource(url: String): void {
			if (url) {
				if (_source.loaded) _source.unload();
				_source.load(new URLRequest(url), app.loaderContext);
			}
		}
	}
}